/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TranSport;

import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


/**
 *
 * @author Administrator
 */
public class TestSocket {
     
 //   public static ObjectInputStream input ;
    public static ObjectOutputStream output;
   public static Socket client;
    
    public static void main(String[] args)
    {
        
        
        
        try
        {
            client = new Socket("localhost", 2003);
            System.out.println("connect thanh cong toi server : "+client.getInetAddress().getHostAddress());
            output = new ObjectOutputStream(client.getOutputStream());
            
        }catch (Exception ex)
        {
            JFrame f = new JFrame();
            JOptionPane.showMessageDialog(f,"khoi tao socket bi loi  : "+ex.getMessage());
        }
        
        String sql = "Select * from SinhVien";
        String database = "QLSinhVien";
        Transport tran = new Transport();
        tran = Send(sql,database);
        System.out.print("nhan du lieu tu server  : ");
        if(tran != null)
        {
            System.out.println("  "+tran.Mesage + tran.errorMessage + "  anh vien ");
            if(tran.Mesage.equalsIgnoreCase("Error"))
            {
                System.out.println("Loi cu phap  : " + tran.errorMessage );
            }
            else if (tran.Mesage.equalsIgnoreCase("OK"))
            {
                System.out.println(" complete : "+tran.errorMessage);
            }
            else
            {
                System.out.println("Noi dung du lieu la : ");
                        for(int i = 0;i<tran.tableKQ.tableData.size();i++)
             {
                 for(int j = 0;j<tran.tableKQ.tableData.elementAt(i).rowData.size();j++)
                 {
                     System.out.print(tran.tableKQ.tableData.elementAt(i).rowData.elementAt(j).columnName + " : " + tran.tableKQ.tableData.elementAt(i).rowData.elementAt(j).Value);
                 }
                 System.out.println();
             }
            }
        }
       
        
        
        
    }
    public static Transport Send(String sql , String database)
    {
        Transport tran = new Transport(sql, " ", null, database);
        try
        {
            output.writeObject(tran);
        }catch(Exception ex)
        {
            JFrame f = new JFrame();
            JOptionPane.showMessageDialog(f, "Error qua trinh write : "+ex.getMessage());
            return null;
        }
        try
        {
            ObjectInputStream input = new ObjectInputStream(client.getInputStream());
            tran.Copy((Transport)input.readObject());
            
        }catch(Exception ex)
        {
            JFrame f = new JFrame();
            JOptionPane.showMessageDialog(f, "Error qua trinh Read : "+ex.getMessage());
            return null;
        }
        return tran;
        /*
        try
        {
            
            Transport transport = new Transport(null, null, null, null);
            transport.Copy((Transport)input.readObject());
            if(transport.Mesage.equalsIgnoreCase("Error"))
            {
                // co loi querry 
                System.out.println( "loi : "+transport.errorMessage);
            }
            else if(transport.Mesage.equalsIgnoreCase("OK"))
            {
                System.out.println("Ok : "+transport.errorMessage);
            }
            else
            {
                for(int i = 0;i<transport.tableKQ.tableData.size();i++)
         {
             for(int j = 0;j<transport.tableKQ.tableData.elementAt(i).rowData.size();j++)
             {
                 System.out.print(transport.tableKQ.tableData.elementAt(i).rowData.elementAt(j).columnName + " : " + transport.tableKQ.tableData.elementAt(i).rowData.elementAt(j).Value);
             }
             System.out.println();
         }
            }
        }catch(Exception ex)
        {
            
        }
         * 
         */
        
    }
   
}
